/**
 * Created by meiguizhinian on 2018/7/20
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.UISlider = ivy.UIBase.extend({
	ctor: function (formName, blockName, sourceControlData) {
		this._super();

		if (formName && blockName && sourceControlData) {
			this.initWith(formName, blockName, sourceControlData);
		}
	},

	initWith : function(formName, blockName, sourceControlData) {
		this.init();
		let uim = ivy.UIManager.getInstance();
		let x = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosX);
		let y = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosY);
		let w = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eWidth);
		let h = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eHeight);
		let layerID = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eLayerID);
		this._layerIDInEditor = layerID;

		this.setSrcRect(cc.rect(x, y, w, h));
		this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x + w / 2.0, y + h / 2.0)));
		this.setSrcPosition(this.getPosition());
		this.setContentSize(cc.size(w, h));
		let anchorType = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Slider_Anchor_list]);
		this.setAnchorType(anchorType);
		this._enterActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Slider_EnterAction_action]));
		this._outActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Slider_OutAction_action]));
		this.setUIElementName(formName, blockName);

		let backBarPath = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Slider_BackBarPath_string]);
		let backBarColor = uim.getControlColorDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Slider_BackBarColor_color]);
		let progressBarPath = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Slider_ProgressBarPath_string]);
		let progressBallPath = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Slider_ProgressBallPath_string]);
		let defaultV = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Slider_DefaultV_int]);
		let maxV = uim.getControlIntDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_Slider_MaxV_int]);

		this._slider = new ccui.Slider();
		this._slider.loadBarTexture(backBarPath);
		this._slider.setCascadeColorEnabled(true);
		this._slider.setCascadeOpacityEnabled(true);
		if (backBarColor === 0) {
			this._slider.getVirtualRenderer().setVisible(false);
		} else {
			let color4 = ivy.Tools.getColor4BFromInt32(backBarColor);
			this._slider.getVirtualRenderer().setColor(cc.color(color4));
			this._slider.getVirtualRenderer().setOpacity(color4.a);
		}
		this._slider.loadSlidBallTextures(progressBallPath);
		this._slider.loadProgressBarTexture(progressBarPath);
		this._slider.setMaxPercent(maxV);
		this._slider.setPercent(defaultV);
		this.addChild(this._slider);
		//////////////////////////////////////////////////////////////////////////
		//设置初始可视性 wsq-2017-3-7
		let isNotVisble = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eNotVisble);
		if (isNotVisble === 1) {
			this.setVisible(false);
		}
	},
	setEventCallback : function(fun) {
		if (this._slider) {
			this._slider.addEventListener(fun);
		}
	},

	setEnableUserTouch : function(touch) {
		this.enableUserTouch_ = touch;
		if (this._slider)
			this._slider.setEnabled(this.enableUserTouch_);
	},

	getMaxPercent : function() {
		if (this._slider)
			return this._slider.getMaxPercent();
		else
			return 0;
	},
	setMaxPercent : function(per) {
		if (this._slider) {
			this._slider.setMaxPercent(per);
		}
	},
	getPercent : function() {
		if (this._slider)
			return this._slider.getPercent();
		else
			return 0;
	},
	setPercent : function(per) {
		if (this._slider) {
			this._slider.setPercent(per);
		}
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	_slider : null,//cocos2d::ui::Slider*
});

ivy.UISlider.create = function (formName, blockName, sourceControlData) {
	return new ivy.UISlider(formName, blockName, sourceControlData);
};
